﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;
using Baimasoft.FrameWork.Base;
using Baimasoft.Manage.SqlServerDAL;
using HsTec.Mobile.Green.SqlServerDAL;

public partial class DocumentManage_Document_SendFile : System.Web.UI.Page
{
    DataFile MyDataFile = new DataFile();
    BaimasoftFile MyFile = new BaimasoftFile();
    SendFile MySendFile = new SendFile();

    LogManage MyLog = new LogManage();


    static public ArrayList hif = new ArrayList(); //保存文件列表

    static public ArrayList aChoiceID = new ArrayList(); //保存列表
    static public ArrayList aChoiceName = new ArrayList(); //保存列表


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            try
            {
                aChoiceID.Clear();
                aChoiceName.Clear();
                FileList.Items.Clear();
                string sChoiceIDCol = Request.QueryString["ChoiceIDCol"].ToString().Trim(); //选择文件ID集合
                string[] aCholceID = sChoiceIDCol.Split('|');
                int iChoiceID = aCholceID.Length;
                for (int i = 0; i < iChoiceID; i++)
                {
                    string sFileName = "";
                    string sDownFilePath = "";    //原文件所在路径
                    string sChoicePath = "";
                    if (aCholceID[i].Length > 0)
                    {
                        DataTable MyDT = MyDataFile.GetDataFileInfo(Convert.ToInt32(aCholceID[i]));  //取各文件的相关信息；
                        foreach (DataRow dr in MyDT.Rows)
                        {
                            sChoicePath = dr["path"].ToString().Trim();
                            sFileName = dr["Name"].ToString().Trim();
                            sDownFilePath = sChoicePath + "\\" + sFileName;
                            aChoiceID.Add(aCholceID[i]);
                            aChoiceName.Add(sFileName); 
                            FileList.Items.Add(sDownFilePath);

                        }

                    }
                }
            }
            catch
            {
            }
        }

    }

    /// <summary>
    /// 删除文档
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DelFile_Click(object sender, EventArgs e)
    {
        if (FileList.SelectedIndex == -1)
        {
            strStatus.Text = "<font color=red>必须指定要删除的文件.</font>";
            return;
        }
        else if (FileList.Items.Count != 0)
        {
            aChoiceID.RemoveAt(FileList.SelectedIndex);
            aChoiceName.RemoveAt(FileList.SelectedIndex);
            FileList.Items.Remove(FileList.SelectedItem.Text);
            strStatus.Text = "";
        }
    }

    /// <summary>
    /// 清空文档
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ClearFile_Click(object sender, EventArgs e)
    {
        aChoiceID.Clear();
        aChoiceName.Clear();
        FileList.Items.Clear();
        strStatus.Text = "";
    }

    /// <summary>
    /// 授权文档
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnSendTo_Click(object sender, EventArgs e)
    {
        if ((string)Session["Baimasoft_Pers_ID"] != null)
        {
            int iSender = int.Parse ((string)Session["Baimasoft_Pers_ID"]);
            string sSenderName = (string)Session["Baimasoft_Pers_Name"];

            string sSendTo = HChoiceID_1.Value.ToString().Trim();
            string sSendToName = ChoiceName_1.Text.ToString().Trim();

            string[] aSendToName = sSendToName.Split(',');
            string[] aSendTo = sSendTo .Split (',');
            int iSendTo = aSendTo.Length ;


            string  sStopDT = StopDT.Text.ToString().Trim();

            if (sStopDT == "")
            {
                StopDT.Focus();
                strStatus.Text = "终止时间不能为空！";
                return;
            }


            string sReadMemo = ReadMemo.Text.ToString().Trim();

            int iChoiceID = aChoiceID.Count;
            if (iChoiceID > 0)
            {
                if (iSendTo > 0)
                {
                    int iback = 0;
                    for (int i = 0; i < iSendTo; i++)
                    {
                        string TempST = aSendTo[i].ToString().Trim();
                        string TempSTName = aSendToName[i].ToString().Trim();

                        for (int j = 0; j < iChoiceID; j++)
                        {
                            int iTempFileID = int.Parse (aChoiceID[j].ToString().Trim());
                            iback = MySendFile.InsertFileSend (iTempFileID, TempST, TempSTName, iSender, sSenderName, Convert.ToDateTime(sStopDT), sReadMemo);
                            iback += iback;

                            try
                            {
                                string sPerID = (string)Session["Baimasoft_Pers_ID"];
                                string sContent = "用户" + (string)Session["Baimasoft_User_LogonID"] + "授权：" + aChoiceName[j].ToString().Trim() + "文档,给：" + TempSTName;
                                string sIP = Page.Request.UserHostAddress;
                                MyLog.InsertLog("3", sPerID, sContent, sIP);
                            }
                            catch
                            {
                                string sPerID = "0";
                                string sContent = "授权：" + aChoiceName[j].ToString().Trim() + "文档,给：" + TempSTName;
                                string sIP = Page.Request.UserHostAddress;
                                MyLog.InsertLog("3", sPerID, sContent, sIP);
                            }
                        }
                    }

                    if (iback > 0)
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('授权成功！');window.close();</script>");
                        return;
                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('授权失败！');window.close();</script>");
                        return;
                    }
                }
                else
                {
                    ChoiceName_1.Focus();
                    strStatus.Text = "被授权人不能为空！";
                    return;
                }
            }
            else
            {
                strStatus.Text = "目前没有可授权的文档！";
                return;
            }
        }
        else
        {
            strStatus.Text = "请重新进入系统，再授权！";
            return;

        }
    }
}
